package com.htl.exampro.question.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htl.exampro.question.mapper.AnswerMapper;
import com.htl.exampro.question.mapper.QuestionMapper;
import com.htl.exampro.question.pojo.constant.QuestionTypeConstant;
import com.htl.exampro.question.pojo.domain.AnswerDO;
import com.htl.exampro.question.pojo.domain.QuestionDO;
import com.htl.exampro.question.service.CheckAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 校对答案服务实现类
 * </p>
 *
 * @author hehehe
 * @since 2025-05-25
 */
@Service
public class CheckAnswerServiceImpl extends ServiceImpl<AnswerMapper, AnswerDO> implements CheckAnswerService {

    @Autowired
    QuestionMapper questionMapper;

    @Override
    public List<AnswerDO> queryAnswer(String size) {
        LambdaQueryWrapper<AnswerDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.isNull(AnswerDO::getScore);
        queryWrapper.last("LIMIT " + size);
        return list(queryWrapper);
    }

    @Override
    public void checkAnswer(List<AnswerDO> answerDOS) {
        Map<Long, List<AnswerDO>> answerDOMap = answerDOS.stream().collect(Collectors.groupingBy(AnswerDO::getQuestionId));
        Set<Long> questionIds = answerDOMap.keySet();
        List<QuestionDO> questionDOS = questionMapper.selectByIds(questionIds);
        for (QuestionDO questionDO : questionDOS) {
            fillScore(answerDOMap.get(questionDO.getQuestionId()), questionDO);
        }
        List<AnswerDO> doList = answerDOS.stream().filter(o -> o.getScore() != null).toList();
        baseMapper.updateList(doList);
    }

    private void fillScore(List<AnswerDO> answerDOS, QuestionDO questionDO) {
        for (AnswerDO answerDO : answerDOS) {
            if ("".equals(answerDO.getAnswer()) || answerDO.getAnswer() == null) {
                answerDO.setScore(0);
                answerDO.setIsCorrect((byte) 0);
                continue;
            }
            answerDO.setScore(answerDO.getFullScore());
            answerDO.setIsCorrect((byte) 1);
            if (QuestionTypeConstant.DANXT.getTypeId().equals(questionDO.getTypeId())
                    || QuestionTypeConstant.PDT.getTypeId().equals(questionDO.getTypeId())
                    || QuestionTypeConstant.TKT.getTypeId().equals(questionDO.getTypeId())) {
                if (!questionDO.getAnswer().equals(answerDO.getAnswer())) {
                    answerDO.setScore(0);
                    answerDO.setIsCorrect((byte) 0);
                }
            } else if (QuestionTypeConstant.DUOXT.getTypeId().equals(questionDO.getTypeId())) {
                if (!questionDO.getAnswer().equals(answerDO.getAnswer())) {//答案不同
                    answerDO.setIsCorrect((byte) 0);
                    List<String> correctAnswer = JSONUtil.toList(questionDO.getAnswer(), String.class);
                    List<String> answer = JSONUtil.toList(answerDO.getAnswer(), String.class);
                    if (correctAnswer.size() > answer.size()) {
                        if (new HashSet<>(correctAnswer).containsAll(answer)) {
                            answerDO.setScore(answerDO.getScore() * answer.size() / correctAnswer.size());
                        } else {
                            answerDO.setScore(0);
                        }
                    } else if (correctAnswer.size() < answer.size()) {
                        answerDO.setScore(0);
                    } else { //答案个数相等
                        if (!new HashSet<>(correctAnswer).containsAll(answer)) {
                            answerDO.setScore(0);
                        }
                    }
                }
            } else if (QuestionTypeConstant.JDT.getTypeId().equals(questionDO.getTypeId())) {
                if (!questionDO.getAnswer().equals(answerDO.getAnswer())) {
                    answerDO.setScore(0);
                    answerDO.setIsCorrect((byte) 0);
                }
            }
        }
    }

}
